package me.android.library.ui.config;


import static org.junit.Assert.assertNotNull;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import mylab.utils.common.IoUtil;
import mylab.utils.common.YamlUtil;

@RunWith(AndroidJUnit4.class)
public class UiConfigUtilTest {

    @Test
    public void loadFromAsset() throws IOException {
        // 获取测试 Context
        android.content.Context context = InstrumentationRegistry.getInstrumentation().getContext();
        // 打开测试 assets 中的文件
        InputStream inputStream = context.getAssets().open("ui.yml");

        // 读取文件内容
        String content = IoUtil.read(inputStream, StandardCharsets.UTF_8);
        inputStream.close();

        System.out.println("### ui.yml ###");
        System.out.println(content);
        assertNotNull(content);

        UiConfig uiConfig = YamlUtil.loadAs(content, UiConfig.class);
        System.out.println("### uiConfig ###");
        System.out.println(uiConfig);
        assertNotNull(uiConfig);
    }

    @Test
    public void loadFromString() {

        UiConfig uiConfig = new UiConfig();
        uiConfig.setMain("mainForm");

        FormInfo fi  = new FormInfo();
        fi.setId("WelcomeForm");
        fi.setHome("Welcome");
        fi.setImplClass("com.sample.app.ui.SimpleLoader");
        uiConfig.getForms().add(fi);

        Yaml yaml = new Yaml();
        // 设置config对象的属性
        String content = yaml.dump(uiConfig);
        System.out.println(content);

        assertNotNull(content);


    }
}